import { type forObject } from '@/types/attach.types'
import {
	type ITask,
	type ITaskForm,
	type ITasks,
	taskScheme,
	tasksScheme
} from '@/types/task.types'

import { axiosWithAuth } from '@/api/interceptors'

export const taskService = {
	async fetchMany(for_object: forObject) {
		const response = await axiosWithAuth.get<ITasks>('/tasks/', {
			params: { [for_object['name']]: for_object['id'] }
		})

		return tasksScheme.parse(response.data)
	},
	async fetch(id: string) {
		const response = await axiosWithAuth.get<ITask>(`/tasks/${id}/`)

		return taskScheme.parse(response.data)
	},
	async update(id: number, data: ITaskForm) {
		const response = await axiosWithAuth.put<ITask>(`/tasks/${id}/`, data)

		return taskScheme.parse(response.data)
	},
	async create(data: ITaskForm) {
		const response = await axiosWithAuth.post<ITask>('/tasks/', data)

		return taskScheme.parse(response.data)
	},
	async del(id: number) {
		const response = await axiosWithAuth.delete(`/tasks/${id}/`)

		return response.status === 204
	}
}
